package com.teddy.customWidgets;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.hp.hpl.jena.rdf.model.Property;
import com.teddy.controller.RDFSchemaControl;
import com.teddy.model.skos.DC;
import com.teddy.model.skos.DCModel;
import com.teddy.util.FileManager;
import com.teddy.view.BaseGUI;
import com.teddy.view.tree.ModelTreeItem;

public class ModelPropertiesGroup extends BasePropertiesGroup{

	private Composite literalComposite;
	//private Group multipleValuesGroup;
	private ModelTreeItem tree; 
	
	public ModelPropertiesGroup(Composite parent, int style, ModelTreeItem tree) {
		super(parent, style);
		this.tree = tree;
		GridLayout gridLayout = new GridLayout(2, true);
		gridLayout.horizontalSpacing = 20;
		setLayout ( gridLayout );
		setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, true) );
		setText( RDFSchemaControl.getResourceString( "metadata.title"));
		
	}


	public void createContent() throws Exception {
		DCModel dcModel = tree.getCore().getDcModel();
		if( dcModel != null){
			literalComposite = new Composite( this, SWT.NONE );
			literalComposite.setLayout(new GridLayout(1, false));
			literalComposite.setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, true));
					
			BaseGUI.setMetaTitle( createPropertiesSingleValue2( literalComposite, 
															"metadata.title", 
															dcModel.getTitle(), 
															false));
			BaseGUI.setMetaAltTitle( createPropertiesSingleValue2( literalComposite, 
															   "metadata.altTitle", 
															   dcModel.getSimpleLiteralValue(DC.alternative), 
															   false ));
			BaseGUI.setMetaDescription( createPropertiesSingleValue2( literalComposite, 
																  "metadata.description", 
																  dcModel.getSimpleLiteralValue(DC.description), 
																  true));
			BaseGUI.setMetaPublisher( createPropertiesSingleValue2( literalComposite, 
																"metadata.publisher", 
																dcModel.getSimpleLiteralValue(DC.publisher), 
																false));
			BaseGUI.setMetaIdentifier( createPropertiesSingleValue2( literalComposite, 
																 "metadata.identifier", 
																 dcModel.getSimpleLiteralValue(DC.identifier), 
																 false));
			BaseGUI.setMetaSource( createPropertiesSingleValue2( literalComposite, 
															 "metadata.source", 
															 dcModel.getSimpleLiteralValue(DC.source), 
															 false));
			
			Button submitBtn = new Button( literalComposite, SWT.NONE);
			submitBtn.setText( "Save changes");
			submitBtn.setToolTipText( "Save changes");
			submitBtn.setLayoutData( new GridData( SWT.END, SWT.FILL, false, false));
			Image img = FileManager.getImage( RDFSchemaControl.getResourceString("icon.save") );
			submitBtn.setImage( img );
			submitBtn.setBounds( img.getBounds());
			
			ExpandBar bar = new ExpandBar( this, SWT.V_SCROLL | SWT.H_SCROLL );
			bar.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, true));
			
			BaseGUI.setMetaExpandBar( bar );
			//TODO - Change actionID
			BaseGUI.setMetaLanguage( createProperties2ColumnMultipleValue( bar, "metadata.language", 
																		   2, dcModel.getLanguages(), 
																		   0, true));
			BaseGUI.setMetaSubject( createProperties2ColumnMultipleValue( bar, "metadata.subject", 
																		  1, dcModel.getSubjects(), 
																		  1, true));
		} else {
			throw new Exception( "Could not load metadata for " + tree.getCore().getName() );
		}
	}
	
	public void refreshMultipleValueProperty(ExpandBar bar, DCModel dcModel, String property ){
		if( dcModel != null ){
			if( property.equals( "language" )) {
				BaseGUI.setMetaLanguage( createProperties2ColumnMultipleValue( bar, "metadata.language", 
						   2, dcModel.getLanguages(), 
						   0, true));
			} else if( property.equals( "subject" )) {
				BaseGUI.setMetaSubject( createProperties2ColumnMultipleValue( bar, "metadata.subject", 
						  1, dcModel.getSubjects(), 
						  1, true));
			}
		}
	}
	
	protected Text createPropertiesSingleValue2( Composite parent, 
			String labelKey, 
			String value, 
			boolean isMultiLine ) {
		Composite propertyComposite = new Composite( parent, SWT.NONE );
		propertyComposite.setLayout(new GridLayout(1, false));
		propertyComposite.setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, false ));
		
		Label label = new Label( propertyComposite, SWT.NONE );
		label.setText( RDFSchemaControl.getResourceString( labelKey + ".label") );
		label.setToolTipText( RDFSchemaControl.getResourceString( labelKey + ".tooltip" ) );
		
		Text editBox;
		GridData gridData;
		if( isMultiLine ) {
			editBox = new Text( propertyComposite, SWT.WRAP | SWT.MULTI | SWT.NONE );
			gridData = new GridData(SWT.FILL, SWT.FILL, true, false);
			gridData.minimumHeight = 50;
			editBox.setLayoutData( gridData );
		} else {
			editBox = new Text( propertyComposite, SWT.NONE );
			editBox.setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, false));
		}
		if(value != null)
			editBox.setText(value);
		editBox.setToolTipText( RDFSchemaControl.getResourceString( labelKey + ".tooltip" ) );
		
		return editBox;
	}
}
